package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.ZoneCode;
import com.softer.wxzj.service.SysOperatorService;
import com.softer.wxzj.service.ZoneCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Niu
 * @create 2019/4/16 - 14:20 - 星期二
 */

/**
 * 实现区域代码的增删改查
 */
@RestController
@RequestMapping("/zoneCode")
@Slf4j
public class ZoneCodeController {

    @Autowired
    ZoneCodeService zoneCodeService;

    @Autowired
    SysOperatorService sysOperatorService;

    /**
     * 添加区域代码信息
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/add")
    @LogRecord(description = "区域代码添加", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.INSERT)
    public CommonResult add(ZoneCode zoneCode) {
        log.info("进入区域代码添加");
        return zoneCodeService.insert(zoneCode);
    }

    /**
     * 添加区域代码信息
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/update")
    @LogRecord(description = "区域代码更改", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.UPDATE)
    public CommonResult update(ZoneCode zoneCode) {
        log.info("区域代码更改");
        return zoneCodeService.update(zoneCode);
    }

    /**
     * 删除区域代码信息  根据id
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/delete")
    @LogRecord(description = "删除区域代码信息", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.DELETE)
    public CommonResult delete(ZoneCode zoneCode) {
        log.info("删除区域代码信息");
        return zoneCodeService.delete(zoneCode);
    }


    /**
     * 查询区域代码信息 带分页
     *
     * @param page
     * @param zoneCode
     * @return
     */
    @RequestMapping("/getList")
    @LogRecord(description = "查询区域代码信息", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public PageInfo<ZoneCode> getList(Page page, ZoneCode zoneCode) {
        log.info("查询区域代码信息");
        return zoneCodeService.getList(page, zoneCode);
    }

    /**
     * 查询区域代码信息  不带分页
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/selectList")
    @LogRecord(description = "查询区域代码信息不带分页", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public List<ZoneCode> selectList(ZoneCode zoneCode) {
        log.info("查询区域代码信息不带分页");
        return zoneCodeService.selectList(zoneCode);
    }

    /**
     * 根据model 查询行数
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/getListCount")
    @LogRecord(description = "查询行数ZoneCode", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public int getListCount(ZoneCode zoneCode) {
        log.info("查询行数ZoneCode");
        return zoneCodeService.getListCount(zoneCode);
    }

    /**
     * 查询全部数据封装成list
     * 获取当前登录人员的区域编码
     * 如果有区域编码
     * 根据区域编码加载区域信息（返回的区域信息已经封装成包含下级的list）
     *
     * @return
     */
    @RequestMapping("/zoneCodeListWhole")
    @LogRecord(description = "查询全部数据封装成list", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public List<ZoneCode> zoneCodeListWhole() {

        log.info("进入zoneCodeListWhole获取当前登录人员的账号查询区域编码");
        //获取当前登录人员的账号  查询区域编码
        String opesNo = ShiroUserUtil.getUserName();
        String ZoneCode = sysOperatorService.getZoneCode(opesNo);
        if (StringUtils.isBlank(ZoneCode)) {
            //没有区域编码时  直接返回null 的list
            log.info("没有区域编码时  直接返回null 的list");
            return null;
        }
        log.info("开始查询区域代码信息根据区域代码例如：12,23");
        return zoneCodeService.zoneCodeListWhole(ZoneCode);
    }

    /**
     * 查询全部数据封装成list
     * 获取当前登录人员的区域编码
     * 如果有区域编码
     * 根据区域编码加载区域信息（返回的区域信息已经封装成包含下级的list）
     *
     * @return
     */
    @RequestMapping("/zoneCodeListWholeOut")
    @LogRecord(description = "查询全部数据封装成list", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public List<ZoneCode> zoneCodeListWholeOut() {
        log.info("进入zoneCodeListWhole获取当前登录人员的账号查询区域编码");
        return zoneCodeService.zoneCodeListWholeOut();
    }


    /**
     * 根据区域编码查询上级区域信息和本区域信息拼接起来返回 区域编码
     *
     * @return
     */
    @RequestMapping("/zoneCodeSuperiors")
    @LogRecord(description = "根据区域编码查询上级区域信息和本机区域代码拼接起来返回", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public String zoneCodeSuperiors(String zoneCode) {
        return zoneCodeService.zoneCodeSuperiors(zoneCode, ShiroUserUtil.getUserName());
    }

    /**
     * 查询区域名称
     *
     * @param zoneCode 区域代码
     * @return 区域名称
     */
    @RequestMapping("/getAllZoneName")
    @LogRecord(description = "根据区域编码查询上级区域信息和本机区域名称拼接起来返回", operationUnit = LogOperationUnit.ZONE_CODE, operationType = LogOperationType.SELECT)
    public String getAllZoneName(String zoneCode) {
        return zoneCodeService.getAllZoneName(zoneCode);
    }
}
